package com.itheima.springbootsecurity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springbootsecurity.domain.Article;
import com.itheima.springbootsecurity.mapper.ArticleMapper;
import com.itheima.springbootsecurity.service.ArticleService;
import com.itheima.springbootsecurity.utils.QueryWrapperUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author faithwu
 * @since 2024-07-12
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Override
    public IPage page(Article article) {
        int page = 1;
        int pageSize = 10;
        if (!Objects.isNull(article.getPage()) && article.getPage() > 0) {
            page = article.getPage();
        }
        if (!Objects.isNull(article.getPageSize()) && article.getPageSize() > 0) {
            pageSize = article.getPageSize();
        }

        IPage<Article> articleIPage = new Page<>(page, pageSize);
        QueryWrapper queryWrapper = QueryWrapperUtil.entity2Wrapper(article);

        IPage result = baseMapper.selectPage(articleIPage, queryWrapper);

        return result;
    }
}
